<?php $this->jQuery()->enable()?>
<?php $this->JQuery()->javascriptCaptureStart()?>
jQuery(function() {
	$j('#container').attr('class', 'profile personal');    
});

<?php $this->JQuery()->javascriptCaptureEnd()?>
<section class="profile">
	<div class="intro">
	    <h1><?php echo $this->user->username?></h1>
   		<p><?php echo $this->t('<em>__1__</em> Views', null, $this->user->getNoViews())?></p>
    	<p class="active_since"><?php echo $this->t('Active Since <em>__1__</em>', null, date(MEDIUM_DATE_FORMAT, $this->user->created_at))?></p>
	</div>
	<div class="info">
		<div class="action">
			<span class="selected"><?php echo $this->t("User's Ads")?></span>
		</div>
	
		<section class="serp">
	    <h2>
			<span>
			<!--<?php echo (($this->page-1) * $this->per_page == 0)? '1':($this->page-1) * $this->per_page?>-<?php echo (($this->listing_number)<29)?$this->listing_number:($this->page * $this->per_page)?> / <b><?php echo number_format($this->listing_number)?> Ads</b>, Page <?php echo number_format($this->page)?> / <?php echo number_format(ceil($this->listing_number/$this->per_page))?>-->
			<?php echo $this->t('__1__-__2__ /__3__ __4__ Ads__5__, Page __6__ / __7__', null, ((($this->page-1) * $this->per_page == 0)? '1':($this->page-1) * $this->per_page), ((($this->listing_number)<29)?$this->listing_number:($this->page * $this->per_page)), '<b>', number_format($this->listing_number), "</b>", number_format($this->page), number_format(ceil($this->listing_number/$this->per_page)))?>
		</span>
		</h2>
<?php 
//next and prev url settter
$url_product = array();
function pager($cur_page_num, $url_raw, $pages){
	$prev = ($cur_page_num>1)? "&page=".($cur_page_num-1):'';
	$next = ($cur_page_num<$pages)? "&page=".($cur_page_num+1):'';
	
	if(strpos($url_raw, '?')===false)
	{
		//echo "1";
		$url_product['prev'] = $url_product['next'] = $url_raw;
		$url_product['prev'] .= ($cur_page_num>1)? str_replace("&","?",$prev):'';
		$url_product['next'] .= ($cur_page_num<$pages)? str_replace("&","?",$next):'';
	}else
	{
		//if(eregi('&page=[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]', $url_raw))// deprecated..lols
		//echo "2";
		if(preg_match('/page=[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]/i', $url_raw))
		{
			//echo "-2.1";
			$url_product['prev'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$prev), $url_raw);
			$url_product['next'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$next), $url_raw);
		}else
		{
			//echo "-2.2";
			/*if(preg_match('/page=[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]/i', $url_raw))
			{
				echo "-2.2.1";
				$url_product['prev'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$prev), $url_raw);
				$url_product['next'] = preg_replace("/page=([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])/", str_replace("&","",$next), $url_raw);
			}else
			{
				echo "-2.2.2";
				$url_product['prev'] = $url_raw.$prev;
				$url_product['next'] = $url_raw.$next;
			}*/
			$url_product['prev'] = $url_raw.$prev;
			$url_product['next'] = $url_raw.$next;
		}
	}
	return $url_product;
}	
		
$pager_url = pager($this->page, $_SERVER["REQUEST_URI"], ceil($this->listing_number/$this->per_page));
$previous_url = $pager_url['prev'];
$next_url = $pager_url['next'];
?>
	    <div class="nav top">
		    <a href="<?php echo $previous_url;?>" class="prev"><span>&lt;</span><?php echo $this->t('PREV')?></a>
		    <form action="<?php echo str_replace(" ","+",$this->user->username);?>" method="get" id="form_sort1">
	            <?php
	            foreach($_GET as $k => $v){
	                if (is_array($v)) {
	                    foreach($v as $k2 => $v2) {
	                        if ($k2 != 'sort') {
	                            ?>
	                            <input type="hidden" name="<?php echo $k.'['.$k2.']'?>" value="<?php echo $v2?>">
	                            <?php
	                        }
	                    }
	                }else {
	                    if ($k != 'sort') {
	                        ?>
	                        <input type="hidden" name="<?php echo $k?>" value="<?php echo $v?>">
	                        <?php
	                    }
	                }
	            }
	            ?>
	
	            <select name="sort" onchange="javascript:if(this.value!='default'){$('#form_sort1').submit();}">
	            	<option value="default"><?php echo $this->t('Sort By...')?></option>
	                <option value="recent" <?php echo !empty($_GET['sort']) && $_GET['sort'] == 'recent' ? 'selected' : ''?>><?php echo $this->t('Most Recent')?></option>
	                <option value="high" <?php echo !empty($_GET['sort']) && $_GET['sort'] == 'high' ? 'selected' : ''?>><?php echo $this->t('Highest Price')?></option>
	                <option value="low" <?php echo !empty($_GET['sort']) && $_GET['sort'] == 'low' ? 'selected' : ''?>><?php echo $this->t('Lowest Price')?></option>
	            </select>
	        </form>
		    <a href="<?php echo $next_url;?>" class="next"><span>&lt;</span><?php echo $this->t('NEXT')?></a>
	    </div>
	    <ul>
	    <?php $i = 0;?>
            <?php foreach($this->list as $listing):?>
            <?php $listing_url = $listing->url[$this->getLanguagePosition()]?>
            	<li>
					<a href="<?php echo $listing_url?>">
						<figure>	
							<img src="<?php echo $this->thumbnail()->scale($listing->photo, 82, 82);?>" alt="<?php echo $this->t('__1__ - photo __2__', null, $listing->title, 1)?>">
						</figure>
						<h1><?php echo $listing->title;//$listing->username?>
						<?php if($listing->sold):?>
                            <?php $tmp_cat = (array)$listing->category_attached; ?>
                            <span class="sold"><?php $tmp = Model_Category::find($tmp_cat[0]); echo $tmp->sold;?></span>
                        <?php endif;?></h1>
						<dl class="location">
                            <?php if(!empty($listing->area_l2)):?>
                                <dd><?php echo Model_Location::find($listing->area_l2)->name?></dd>
                            <?php endif;?>
                        </dl>
						<dl class="date">
							<dd><?php echo date(MEDIUM_DATE_FORMAT, strtotime($listing->posted_at))?></dd>
						</dl>
						<dl class="price">
							<dd>
								<?php if($listing->sold):?>
									<?php echo $this->t('Sold')?>
								<?php else:?>
									<?php if ($i==0):?><dt><span><?php echo $this->t('Price');?></span></dt><?php endif;?>
		                            <?php if (!empty($listing->sale_price) || !empty($listing->rent_price)): ?>
		                                <?php
		                                if (!empty($listing->sale_price)) {
		                                    $sale_price = number_format($listing->sale_price);
		                                    ?><dd>HKD <?php echo $sale_price?> <em> <?php echo $this->t('(Sale)');?></em></dd><?php
		                                }
		                                if (!empty($listing->rent_price)) {
		                                    $rent_price = number_format($listing->rent_price);
		                                    ?><dd>HKD <?php echo $rent_price?> <em> <?php echo $this->t('(Rent)');?></em></dd><?php
		                                }
		                                ?>   
		                                    
		                            <?php elseif (!empty($listing->is_free)):?>
		                                <dd><?php echo $this->t('Free');?></dd>
		                            <?php elseif (!empty($listing->is_contact)):?>
		                                <dd><?php echo $this->t('Please contact');?></dd>
		                            <?php elseif (!empty($listing->price)): ?>
		                                <dd>HKD <?php echo number_format($listing->price);?></dd>
		                            <?php endif;?>
		                        <?php endif;?>
							</dd>
						</dl>
						<span>&gt;</span>
					</a>
				</li>
                <?php $i++;?>
            <?php endforeach;?>
	    
	    <div class="nav bottom">
	    <a href="<?php echo $previous_url;?>" class="prev"><span>&lt;</span><?php echo $this->t('PREV')?></a>
	    <!--p>Page <?php echo number_format($this->page)?> / <?php echo number_format(ceil($this->listing_number/$this->per_page))?></p-->
	    <p><?php echo $this->t('Page __1__ / __2__', null, number_format($this->page), number_format(ceil($this->listing_number/$this->per_page)))?></p>
		<a href="<?php echo $next_url;?>" class="next"><span>&lt;</span><?php echo $this->t('NEXT')?></a>
	    </div>
	    </section>

	</div>
	

    </section>

    <?php echo $this->Partial('mobile/other.phtml', 'default'); ?>
